//建立文件applycation的数据集
const applycationType = {
  txt: "text/plain",
  doc: "application/msword",
  docx: "application/vnd.openxmlformats-officedocument.wordprocessingml.document",
  pdf: "application/pdf",
  xls: "application/vnd.ms-excel",
  xlsx: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  xlsm: "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet",
  xlm: "application/vnd.ms-excel",
  ppt: "application/vnd.ms-powerpoint",
  pptx: "application/vnd.openxmlformats-officedocument.presentationml.presentation",
  wps: "application/vnd.ms-works",
  tgz: "application/x-compressed",
  tar: "application/x-tar",
  zip: "application/zip",
  bmp: "image/bmp",
  gif: "image/gif",
  jpe: "image/jpeg",
  jpeg: "image/jpeg"
};

export default function (res, fileType, filename) {
  //blob   后台传递的文件流
  //filename 导出文件命名

  if (!res || !fileType) return;
  let fileApplycation = applycationType[fileType];
  try {
    const blob = new Blob([res], {
      type: fileApplycation
    });
    const link = document.createElement("a");
    link.href = window.URL.createObjectURL(blob);
    const format = new Date().getTime();
    link.download = filename ? `${filename}-${format}.xlsx` : `${format}.xlsx`;
    document.body.appendChild(link);
    link.click();
    window.URL.revokeObjectURL(link.href);
    document.body.removeChild(link);
  } catch (e) {
    alert("导出数据出错");
  }
}
